/*
 * $Id: SimpleCodeGenerator.java 42 2013-04-17 15:33:59Z gabakyan $
 * $Author: gabakyan $
 * $Revision: 42 $
 * $Date: 2013-04-17 15:33:59 +0000 (Wed, 17 Apr 2013) $
 *
 * Copyright (c) 2013 Supply Chain Intelligence (SCI), Inc.
 * http://www.scintelligence.com/, Email: info@scintelligence.com
 * All rights reserved.
 *
 * This file is part of Logistics Map.
 *
 * Logistics Map is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * Logistics Map is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Logistics Map.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sci.logisticsmap.service.support;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * The class <code>SimpleCodeGenerator</code> provides the simple code generation method.
 *
 * @author gabakyan@gmail.com (Last modified by $Author: gabakyan $)
 * @version $Revision: 42 $
 */
@Component
public class SimpleCodeGenerator {
    /**
     * The default length.
     */
    public static final int DEFAULT_LENGTH = 32;

    /**
     * The symbols.
     */
    private static final char[] symbols = new char[10 + 26 + 26];

    /**
     * The static initialization block.
     */
    static {
        int j = 0;
        for (int i = 0; i < 10; ++i) {
            symbols[j++] = (char) ('0' + i);
        }
        for (int i = 0; i < 26; ++i) {
            symbols[j++] = (char) ('a' + i);
        }
        for (int i = 0; i < 26; ++i) {
            symbols[j++] = (char) ('A' + i);
        }
    }

    /**
     * The random numbers generator.
     */
    private final Random random = new Random();
    /**
     * The symbols buffer length.
     */
    private final int length;

    /**
     * The default constructor.
     * Creates a new simple code generator with the default length.
     */
    public SimpleCodeGenerator() {
        this(DEFAULT_LENGTH);
    }

    /**
     * Creates a new simple code generator with the given length.
     *
     * @param length The length.
     */
    public SimpleCodeGenerator(@Value("${codeGenerator.length}") int length) {
        int len = 1;
        if (length > 1) {
            len = length;
        }
        this.length = len;
    }

    /**
     * Generates the next simple code string.
     *
     * @return The next simple code string.
     */
    public String nextString() {
        final char[] buffer = new char[length];

        for (int i = 0; i < buffer.length; ++i) {
            buffer[i] = symbols[random.nextInt(symbols.length)];
        }

        return new String(buffer);
    }
}
